function showDiv(html) {
	var div = $("#showmessage");	
	var height = (window.screen.availHeight/2-$("#showmessage").height());
	var width = (window.screen.availWidth - div.width()) / 2 ;	
	div.html(html).css( {left : width,	top :  height}).show();
}
function hideDiv(){
	var div = $("#showmessage");	
	div.html('').hide();
}
function getTxTotal(startYear, tx) {
	$.ajax( {
		url : getActionUrl("getTxTotal"),
		data : {
			'startYear' : startYear,
			'tx' : tx
		},
		dataType : 'json',
		beforeSend:function(){
			showDiv("<br/><br/><br/><br/>正在处理中，请稍后……");
		},
		success : function(jArr) {
			hideDiv();
			if(jArr==null){
				alert("您没有权限执行此操作！");
				return;
			}
			var thead = "";
			thead += "<tr><td ></td><td ></td>";
			for ( var i = 1; i <= 12; i++) {
				thead += "<td colspan='12'>" + i + "月</td>";
			}
			thead += "<td colspan='12'>合计</td><td colspan='5'>用药比例</td></tr>";

			thead += "<tr><td ></td><td ></td>";
			for ( var i = 1; i <= 13; i++) {
				thead += "<td colspan='6'>总数</td><td colspan='6'>活体</td>";
			}
			thead += "<td colspan='5'>总数</td></tr><tr><td >区域</td><td >医院</td>";
			for ( var i = 1; i <= 26; i++) {
				thead += "<td>PRG</td><td>华东TAC</td><td>海正TAC</td><td>CsA</td><td>Siro</td><td>total</td>";
			}
			thead += "<td>PRG</td><td>华东TAC</td><td>海正TAC</td><td>CsA</td><td>Siro</td></tr>";

			$("#txTable").append(thead);
			var tr = "";
			for ( var i = 0; i < jArr.length; i++) {
				for ( var h = 0; h < jArr[i].hospital.length; h++) {
					var color = (i * 5000).toString(16);
					tr += "<tr bgcolor='#F0F1F2'><td >" + jArr[i].areaName + "</td><td >" + jArr[i].hospital[h].hospitalName + "</td>";
					for ( var d = 0; d < jArr[i].hospital[h].data.length; d++) {
						for ( var t = 0; t < jArr[i].hospital[h].data[d].P112Total.length; t++) {
							tr += "<td>" + jArr[i].hospital[h].data[d].P112Total[t] + "</td>";
						}
						for ( var t = 0; t < jArr[i].hospital[h].data[d].LP112Total.length; t++) {
							tr += "<td>" + jArr[i].hospital[h].data[d].LP112Total[t] + "</td>";
						}
						if (jArr[i].hospital[h].data[d].totalPercent != '') {
							for ( var t = 0; t < jArr[i].hospital[h].data[d].totalPercent.length; t++) {
								tr += "<td>" + jArr[i].hospital[h].data[d].totalPercent[t] + "</td>";
							}
						}
					}
					tr += "</tr>"
				}
			}
			$("#txTable").append(tr);			
		},error:function(XMLHttpRequest, textStatus, errorThrown){
			hideDiv();
			alert("网络故障，请稍后再试！\ntextStatus："+textStatus+"\nerrorThrown："+errorThrown);
		}
	});
}
function getTxTotal2(startYear, tx) {
	$.ajax( {
		url : getActionUrl("getTxTotal2"),
		data : {
			'startYear' : startYear,
			'tx' : tx
		},
		dataType : 'json',
		success : function(jArr) {
			hideDiv();
			if(jArr==null){
				alert("您没有权限执行此操作！");
				return;
			}
			var thead = "";
			thead += "<tr><td ></td><td ></td>";
			thead += "<td colspan='12'>合计</td><td colspan='5'>用药比例</td></tr>";

			thead += "<tr><td ></td><td ></td>";
			thead += "<td colspan='6'>总数</td><td colspan='6'>活体</td>";
			thead += "<td colspan='5'>总数</td></tr><tr><td>区域</td><td>医院</td>";
			for ( var i = 1; i <= 2; i++) {
				thead += "<td>PRG</td><td>华东TAC</td><td>海正TAC</td><td>CsA</td><td>Siro</td><td>total</td>";
			}
			thead += "<td>PRG</td><td>华东TAC</td><td>海正TAC</td><td>CsA</td><td>Siro</td></tr>";

			$("#txTable").append(thead);
			var tr = "";
			for ( var i = 0; i < jArr.length; i++) {
				for ( var h = 0; h < jArr[i].hospital.length; h++) {
					var color = (i * 5000).toString(16);
					tr += "<tr bgcolor='#F0F1F2'><td>" + jArr[i].areaName + "</td><td>" + jArr[i].hospital[h].hospitalName + "</td>";
					for ( var t = 0; t < jArr[i].hospital[h].P112Total.length; t++) {
						tr += "<td>" + jArr[i].hospital[h].P112Total[t] + "</td>";
					}
					for ( var t = 0; t < jArr[i].hospital[h].LP112Total.length; t++) {
						tr += "<td>" + jArr[i].hospital[h].LP112Total[t] + "</td>";
					}

					for ( var t = 0; t < jArr[i].hospital[h].totalPercent.length; t++) {
						tr += "<td>" + jArr[i].hospital[h].totalPercent[t] + "</td>";
					}

					tr += "</tr>"
				}
			}
			$("#txTable").append(tr);
		},error:function(XMLHttpRequest, textStatus, errorThrown){
			hideDiv();alert("网络故障，请稍后再试！\ntextStatus："+textStatus+"\nerrorThrown："+errorThrown);
		}
	});
}

window.onload = function() {
	var str = "";
	for ( var i = new Date().getFullYear(); i >= 2006; i--) {
		str += "<option value=" + i + ">" + i + "</option>";
	}
	$("select[name='startYear']").append(str);
	$("select[name='tx']").append("<option value=" + 1 + ">" + "肝" + "</option>");
	$("select[name='tx']").append("<option value=" + 2 + ">" + "肾" + "</option>");
	$("select[name='tx']").append("<option value=" + 3 + ">" + "心" + "</option>");
	$("select[name='tx']").append("<option value=" + 4 + ">" + "肺" + "</option>");
	$("select[name='tx']").append("<option value=" + 5 + ">" + "小肠" + "</option>");
	$("select[name='tx']").append("<option value=" + 6 + ">" + "其它" + "</option>");
	document.getElementsByName("btn1")[0].onclick = function() {		
		var startYear = document.getElementsByName('startYear')[0].value;
		var tx = document.getElementsByName('tx')[0].value;		
		getTxTotal(startYear, tx);
	}
	document.getElementsByName("btn2")[0].onclick = function() {
		var startYear = document.getElementsByName('startYear')[0].value;
		var tx = document.getElementsByName('tx')[0].value;
		$.ajax( {
			url : getActionUrl("checkForDownLoad", "startYear=" + startYear + "&tx=" + tx),
			type : "post",
			dataType : 'json',
			success : function(obj) {		
				if (obj.status == 'ok') {
					window.location.href = getActionUrl("downLoadFile", "startYear=" + startYear + "&tx=" + tx);
				} else if(obj.status =='wait'){
					$.ajax( {
						url : getActionUrl("getTxTotal", "startYear=" + startYear + "&tx=" + tx),
						type : "post",
						dataType : 'json',
						beforeSend:function(){
							showDiv("<br/><br/><br/><br/>正在处理中，请稍后……");
						},
						success : function() {
							hideDiv();
							window.location.href = getActionUrl("downLoadFile", "startYear=" + startYear + "&tx=" + tx);
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							hideDiv();alert("下载失败，请稍后再试！\ntextStatus："+textStatus+"\nerrorThrown："+errorThrown);
						}
					});
				}else{
					hideDiv();alert("您没有权限进行此操作！");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideDiv();alert("网络故障，请稍后再试！\ntextStatus："+textStatus+"\nerrorThrown："+errorThrown);
			}
		});
	}
	}

